Next | Prev | Up | Top | Contents | Index

Determining Extension Availability

This section starts with an Extension Suffix Overview, then provides information on How to Check for OpenGL Extension Availability and an Example Program: Checking for Extension Availability.

Extension Suffix Overview

Function names and tokens for OpenGL extensions have EXT or a vendor-specific acronym as a suffix, for example glVertexPointerEXT() or glColorTableSGI(). The names of the extensions themselves (the extension strings) use prefixes, for example, SGI_color_table. Below is a detailed list of all suffixes and prefixes:

How to Check for OpenGL Extension Availability

All supported extensions have a corresponding definition in gl.h and a token in the extensions string returned by glGetString(). For example, if the vertex array extension (EXT_vertex_array) is supported, it is defined in gl.h as follows:

#define SGI_COMPILED_VERTEX_ARRAY 1

GL_SGI_compiled_vertex_array appears in the extensions string returned by glGetString(). Use the definitions in gl.h at compile time to determine if procedure calls corresponding to an extension exist in the library.

Applications should do compile-time checking--for example, making sure GL_SGI_compiled_vertex_array is defined; and run-time checking--for example, making sure GL_SGI_compiled_vertex_array is in the extension string returned by glGetString().

Don't call glGetString() until a rendering context has been bound. When rendering under the X Window System, it's critical to know what kind of machine the display is on because different X displays can have different capabilities. While this may not be so critical under Windows, it's important not to forget it for portability reasons.

The function wglGetProcAddress() returns the address of an OpenGL extension function to use with the current OpenGL rendering context. For example, to use the extension command glVertexPointerEXT(), an application can obtain the address of the function to call by calling wglGetProcAddress() by following these steps:

  1. Call wglGetProcAddress() to obtain the function pointer type definitions for each function from the header file GL/gl.h:

    typedef void (APIENRY *PFNGLVERTEXPOINTEREXTPROC)

    (GLint size, GLenum type, glsizei stride,

    GLsizei count, const GLvoid *pointer);

  2. Use the definition obtained in step 1 to declare a pointer to store the result of the call to wglGetProcAddress():

    PFNGLVERTEXPOINTEREXTPROC glVertexPointerEXT;


glVertexPointerEXT =

(PFNGLVERTEXPOINTEREXTPROC)

wglGetProcAddress ("glVertexPointerEXT");

  1. Use the pointer declared in step 2 to execute the command:

    (*glVertexPointerEXT)(3, GL_FLOAT, 0, 0, data);

Example Program: Checking for Extension Availability

In Example 3-1, the function QueryExtension() checks whether an extension is available.

Example 3-1 : Checking for Extensions

main(int argc, char* argv[]) {
...
    if (!QueryExtension("GL_EXT_paletted_texture")) {
        fprintf(stderr, "paletted texture extension not supported.\n");
         exit(1);
    }
...
}

static GLboolean QueryExtension(char *extName)
{
    /*
    ** Search for extName in the extensions string. Use of strstr()
    ** is not sufficient because extension names can be prefixes of
    ** other extension names. Could use strtok() but the constant
    ** string returned by glGetString might be in read-only memory.
    */
    char *p;
    char *end;
    int extNameLen;   

    extNameLen = strlen(extName);
        
    p = (char *)glGetString(GL_EXTENSIONS);
    if (NULL == p) {
        return GL_FALSE;
    }

    end = p + strlen(p);   

    while (p < end) {
        int n = strcspn(p, " ");
        if ((extNameLen == n) && (strncmp(extName, p, n) == 0)) {
            return GL_TRUE;
        }
        p += (n + 1);
    }
    return GL_FALSE;
}

Extension Suffix Overview
How to Check for OpenGL Extension Availability
Example Program: Checking for Extension Availability

Next | Prev | Up | Top | Contents | Index